package station;

import sourceNsink.*;

public class Starten {

	
	
	public static void main(String[] args) {
		
		
		
		if(args.length < 7){
			System.out.println("Usage: Starten <MCastAddress>  <DataSourcePort> <DataSinkPort> <MulticastPort> <TeamNr> <StationNr> <Runtime> <NetworkInterface>(optional)");
			return;
		}
		String mCastAddress = args[0];		//Empfänger und Senderthread
		int sourcePort = Integer.parseInt(args[1]);			//UDPWorker und DataSource
		int sinkPort = Integer.parseInt(args[2]);			//Empf�ngerthread und DataSink
		int MultiCastPort = Integer.parseInt(args[3]);
		int teamNr = Integer.parseInt(args[4]);			//TeamNr f�r Sender- und Empf�ngerthread MultiCastport
		//int ourPort = MultiCastPort + teamNr;
		int station = Integer.parseInt(args[5]);			//StationsNummer
		int runtime = Integer.parseInt(args[6]);			//runtime in Sekunden
		String netwInterface;
		if(args.length == 7){
			netwInterface = "";			
		}else{
			netwInterface = args[7];						//network Interface ... optional
		}
		
//		int sourcePort = 7773;			//UDPWorker und DataSource
//		int sinkPort = 8883;			//Empf�ngerthread und DataSink
//		int MultiCastPort = 15000;	
//		int teamNr = 14;			//TeamNr f�r Sender- und Empf�ngerthread MultiCastport
//		//int ourPort = MultiCastPort + teamNr;
//		int station = 5;			//StationsNummer
//		int runtime = 10;			//runtime in Sekunden
//		String netwInterface = "";
		
		
		System.out.println("Legende: \nST = Senderthread\nET = Empfaengerthread\n------------------------------------------------------------\n");
		
		DataSink dsink = new DataSink(teamNr, station, sinkPort);
		
		String input = "team " + teamNr + "-" + station;
		DataSource dsource = new DataSource(input, 1000, sourcePort);
		
		SenderThread st = new SenderThread(mCastAddress, MultiCastPort, netwInterface);
		TimerThread tt = new TimerThread(st);
		EmpfaengerThread et = new EmpfaengerThread(mCastAddress, st, MultiCastPort, station, sinkPort, teamNr, netwInterface);
		UDPWorker ut = new UDPWorker(st, sourcePort);
				
		Thread t5 = new Thread(dsource);       //DataSource
        t5.start();
		Thread t6 = new Thread(dsink);		//DataSink
		t6.start();
		
		System.out.println("Source und Sink gestartet...");
		try {							//1 Sekunde Vorlauf f�r Sink und Source geben
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
		
		Thread t4 = new Thread(ut);       //UDP-Thread (Worker)
        t4.start();
		Thread t1 = new Thread(et);		//Empf�ngerthread
		t1.start();
		Thread t2 = new Thread(tt);		//Timerthread
		t2.start();
		Thread t3 = new Thread(st);		//Senderthread
		t3.start();	
		
		try {					//runtime abwarten
			Thread.sleep(runtime*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Shutting station [" + station + "] down:");
		et.terminate();
		st.terminate();
		tt.terminate();
		ut.terminate();
		dsource.terminate();
		dsink.terminate();
	}

}
